{% extends 'layout.html' %}
{% block css %}
    <style>
        table td{
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
{% endblock %}
{% block main %}
<div class="page-header">
    <h1>
        数据库集群关联信息
    </h1>
</div><!-- /.page-header -->
<div class="row">
    <div class="col-xs-12">
        <div class="row" style="margin-bottom: 10px;">
            <div class="col-xs-6">
                ID:{{ci._id}}
            </div>
            <div class="col-xs-6">
                集群名称:{{ci.db_cluster_name}}
            </div>
        </div>
        <div class="row" style="margin-bottom: 10px;">
            <div class="col-xs-12">
                <span style="font-size: 20px;">
                    节点列表
                </span>
            </div>
        </div>
        <div class="row" style="margin-bottom: 10px;">
            <form id="data_form">
                <div class="col-xs-12">
                    <div class="navbar-header">
                        <span class="navbar-brand">服务器:</span>
                    </div>
                    <div class="nav navbar-nav form-group">
                        <div class="navbar-form navbar-left">
                            <div class="form-group">
                                <select id="new_server" style="width:150px;" class="select2 user-select2 form-control" data-placeholder="">
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="navbar-header">
                        <span class="navbar-brand">节点名称:</span>
                    </div>
                     <div class="nav navbar-nav form-group " style="width: 300px">
                        <div class="navbar-form navbar-left ">
                            <div class="form-group" >
                                <input id="node_name" type="text" style="width: 250px">
                            </div>
                        </div>
                    </div>
                    <div class="navbar-header">
                        <span class="navbar-brand">节点类型:</span>
                    </div>
                    <div class="nav navbar-nav form-group">
                        <div class="navbar-form navbar-left">
                            <div class="form-group">
                                <select id="db_role" style="width:100px;" class="select2" data-placeholder="选择角色" >
                                    <option value="main_db">主库</option>
                                    <option value="f_ass_db">第一从库</option>
                                    <option value="s_ass_db">第二从库</option>
                                    <option value="r_ass_db">容灾节点</option>
                                </select>
                                <button class="btn btn-xs btn-success"
                                    onclick="createRelation({{ci_id}})">新增节点</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <table class="table table-bordered table-striped table-condensed"
                    style="table-layout: fixed">
                    <thead>
                        <tr>
                            <th style="text-align: center; width: 80px;">id</th>
                            <th>节点名称</th>
                            <th>节点类型</th>
                            <th>服务器IP</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for second_ci in second_cis %}
                        <tr>
                            <td><div style="text-align: center; white-space: nowrap;">{{second_ci._id}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{second_ci.hostname}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">
                                {% if second_ci.relation_type == 'main_db' %}
                                主库
                                {% elif second_ci.relation_type=='f_ass_db' %}
                                第一从库
                                {% elif second_ci.relation_type=='s_ass_db'%}
                                第二从库
                                {% elif second_ci.relation_type=='r_ass_db'%}
                                容灾节点
                                {% endif %}
                            </div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{second_ci.private_ip.0}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">
                                    <button class="btn btn-xs btn-danger"
                                        onclick="deleteRelation({{ci_id}},{{second_ci._id}})">删除关联</button>
                                </div></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        <div class="row" style="margin-bottom: 10px;">
            <div class="col-xs-12">
                <span style="font-size: 20px;">
                    操作日志
                </span>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <table class="table table-bordered table-striped table-condensed"
                    style="table-layout: fixed;">
                    <thead>
                        <tr>
                            <th style="text-align: center; width: 50px;">ID</th>
                            <th style="text-align: center; width: 200px;">操作者</th>
                            <th style="text-align: center; width: 120px;">操作类型</th>
                            <th style="text-align: center; width: 150px;">操作时间</th>
                            <th style="text-align: center;">详细信息</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for log in logs %}
                        <tr>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.id}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.operator}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.command}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.create_time}}</div></td>
                            <td><div style="text-align: center; white-space: nowrap;">{{log.content}}</div></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block script %}
<script src="/static/js/chosen.jquery.min.js"></script>
<script src="/static/js/jquery-ui-1.10.3.full.min.js"></script>
<script src="/static/js/jquery.ui.touch-punch.min.js"></script>
<script type="text/javascript" src="/static/js/select2.min.js"></script>
<script src="/static/js/all-config.js"></script>
<script src="/static/js/sweetalert.min.js"></script>
<script>
    var createRelation = function(first_id){
        var saveData = {
            'first_id': first_id,
            'second_id': $('#new_server').val(),
            'db_role': $('#db_role').val(),
            'node_name': $('#node_name').val()
        };
        $.ajax({
            url: "/db/db_cluster/createNode",
            type: "POST",
            data: JSON.stringify(saveData),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (data.success){
                    location.reload(true);
                }else{
                    location.reload(true);
                    alert(data.message);
                }
            }
        });
    };
    var deleteRelation = function(first_id, second_id){
        ret = window.confirm("确认要删除关联关系?");
        if (!ret) {
            return;
        }

        var saveData = {
            'first_id': first_id,
            'second_id': second_id
        };
        $.ajax({
            url: "/cmdb/relation/delete",
            type: "POST",
            data: JSON.stringify(saveData),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (data.redirect) {
                    window.location.href = data.redirect;
                    return;
                }
                if (data.success){
                    location.reload(true);
                }else{
                    swal("删除关联失败");
                }
            }
        });
    };
    var select2_option = {
        ajax: {
            url: "/cmdb/query/selectAllServer",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    q: params.term
                };
            },
            processResults: function (data, page) {
                return {
                    results: data.items
                };
            },
            cache: true
        },
        allowClear: true,
        minimumInputLength: 2
    };
    $("#new_server").select2(select2_option);
</script>
{% endblock %}